package com.nine.controller;

import com.github.pagehelper.Page;
import com.nine.entity.MyReturn;
import com.nine.entity.PageSort;
import com.nine.entity.TopicFpa;
import com.nine.entity.Userinfo;
import com.nine.service.impl.TopicFpaServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:FPA测试体的控制层
 * @Author:LouFangRui
 * @Date:2022/6/29 17:26
 */
@RestController
@RequestMapping("fpa")
public class TopicFpaController extends ControllerFather{
	@Autowired
	private TopicFpaServiceImpl topicFpaService;
	MyReturn myReturn=new MyReturn();

	/**
	 * 删除和批量删除
	 */
	@RequestMapping("deleteFpaQuestion.do")
	public MyReturn deleteFpaQuestion(String [] topicId){
		int i = topicFpaService.deleteById(topicId);
		if (i>0){
			return super.getSuccessResult(myReturn);
		}else {
			return super.getFailResult(myReturn);
		}
	}
	/**
	 * 分页查询and模糊查询
	 */
	@RequestMapping("findAllFpaQuestion.do")
	public MyReturn findAllFpaQuestion(PageSort pageSort, TopicFpa topicFpa){
		Page page=topicFpaService.selectAllQues(pageSort,topicFpa);
		//获取分页之后的数据并向前端传输
		myReturn.setData(page.getResult());
		//获取数据总条数并向前端传输
		myReturn.setCount(page.getTotal());
		return super.getSuccessResult(myReturn);
	}
	/**
	 * 添加
	 * */
	@RequestMapping("addFpaQuestion.do")
	public MyReturn addFpaQuestion(TopicFpa topicFpa,HttpSession httpSession){

		if(topicFpa.getStates()!=null){
			topicFpa.setStates("0");
		}else{
			topicFpa.setStates("1");
		}

		//获取登录者信息，并保存到实体中
		Userinfo userInfo = (Userinfo) httpSession.getAttribute("userInfo");
		String userName = userInfo.getUserName();
		topicFpa.setCreateBy(userName);

		// 当前添加时间
		topicFpa.setCreateTime(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())));
		int i = topicFpaService.addFpaQuestion(topicFpa);
		if (i>0){
			return super.getSuccessResult(myReturn);
		}else{
			return super.getFailResult(myReturn);
		}
	}
	/**
	 * 修改
	 * */
	@RequestMapping("updateFpaQuestion.do")
	public MyReturn updateFpaQuestion(TopicFpa topicFpa, HttpSession httpSession){

		if(topicFpa.getStates()!=null){
			topicFpa.setStates("0");
		}else{
			topicFpa.setStates("1");
		}
		//获取登录者信息，并保存到实体中
		Userinfo userInfo = (Userinfo) httpSession.getAttribute("userInfo");
		String userName = userInfo.getUserName();
		topicFpa.setUpdateBy(userName);

		// 当前修改时间
		topicFpa.setUpdateTime(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())));
		int i = topicFpaService.updateFpaQuestion(topicFpa);
		System.out.println(topicFpa.toString());
		if (i>0){
			return super.getSuccessResult(myReturn);
		}else{
			return super.getFailResult(myReturn);
		}
	}
	/**
	 * 查询全部
	 * */
	public MyReturn selectAll(TopicFpa topicFpa){
		List<TopicFpa> topicFpaList = topicFpaService.selectAllQues(topicFpa);
		return super.getSuccessResult(myReturn);
	}
}
